package com.ruoyi.tenant.service.impl;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.framework.datasource.DynamicDataSource;
import com.ruoyi.framework.datasource.DynamicDataSourceContextHolder;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.tenant.domain.SysTenant;
import com.ruoyi.tenant.mapper.SysTenantMapper;
import com.ruoyi.tenant.service.ISysTenantService;
import com.ruoyi.tenant.utils.AddDataSourceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service("ISysTenantService")
@DataSource(DataSourceType.MASTER)
public class SysTenantServiceImpl implements ISysTenantService {
    @Autowired
     private SysTenantMapper sysTenantMapper;
    @Autowired
     private ISysUserService sysUserService;
    @Autowired
    private AddDataSourceUtil addDataSourceUtil;

     @Override
     public SysTenant selectTenantByTenantId(String TenantId){
         return sysTenantMapper.selectTenantByTenantId(TenantId);
     }

     @Override
    public int createdatabase(String databasename){
         return sysTenantMapper.createdatabase(databasename);
     }

     @Override
    public Long insertSysTenant(SysTenant sysTenant){
         return sysTenantMapper.insertSysTenant(sysTenant);
    };

     @Override
    public Integer selectTenantStateByTenantId(String TenantId){return sysTenantMapper.selectTenantStateByTenantId(TenantId);}

    @Override
    public SysTenant selectSysTenantById(Integer id)
    {
        DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.MASTER.name());
            SysTenant re = sysTenantMapper.selectSysTenantById(id);
        DynamicDataSourceContextHolder.clearDataSourceType();
        addDataSourceUtil.addDataSource(re);

        //查找租户数据库超管
        DynamicDataSourceContextHolder.setDataSourceType(re.getTenantid());
            SysUser adminUser = sysUserService.selectUserById(1L);
        DynamicDataSourceContextHolder.clearDataSourceType();
        re.setUsername(adminUser.getUserName());
        re.setPassword(adminUser.getPassword());

        return re;

    }


    @Override
    public List<SysTenant> selectSysTenantList(SysTenant sysTenant)
    {
        return sysTenantMapper.selectSysTenantList(sysTenant);
    }


    @Override
    public int updateSysTenant(SysTenant sysTenant)
    {
        int update = 0;

        DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.MASTER.name());
          update = sysTenantMapper.updateSysTenant(sysTenant);
          SysTenant link = sysTenantMapper.selectTenantByTenantId(sysTenant.getTenantid());
        DynamicDataSourceContextHolder.clearDataSourceType();

        if(sysTenant.getChangePsd()) {
            addDataSourceUtil.addDataSource(link);
            DynamicDataSourceContextHolder.setDataSourceType(link.getTenantid());
            update = sysUserService.resetUserPwd(sysTenant.getUsername(), SecurityUtils.encryptPassword(sysTenant.getPassword()));
            DynamicDataSourceContextHolder.clearDataSourceType();
        }
        return update;
    }


    @Override
    public int deleteSysTenantByIds(Integer[] ids)
    {
        return sysTenantMapper.deleteSysTenantByIds(ids);
    }


    @Override
    public int deleteSysTenantById(Integer id)
    {
        return sysTenantMapper.deleteSysTenantById(id);
    }

}
